import { reactive, type Ref, ref } from "vue";
// import type { ListOption } from "../../../api/k8s/types";

import yaml2obj from "js-yaml";
import json2yaml from "js-yaml";

export class Pager {
  // 当前页码
  currentPage = ref(1);

  // 每页数量
  limit = ref(10);
  limitList = ref([10, 20, 50]);

  // 总数
  total = ref(0);

  setTotal(total: number) {
    this.total.value = total;
  }
}

export class SourceMan<T = any> {
  // 资源列表
  protected srcList = ref<T[]>();

  // 资源详情
  srcDetail = ref<T>();

  currentSrc = ref<T>();

  // 资源yaml
  srcYamlContent = ref<string>();
  yamlDialog = ref(false);

  // 搜索内容
  searchQuery = reactive<Record<string, any>>({});

  // loading
  loading = ref(false);

  // 分页
  pager = new Pager();

  public get nodeTotal(): Ref<number> {
    return this.pager.total;
  }

  protected continue?: string;

  // #### UTILS ####
  // 类型转换
  objectToYaml = (obj: any) => {
    return json2yaml.dump(obj);
  };

  // yaml转换
  yamlToObject = (yaml: string) => {
    return yaml2obj.load(yaml);
  };

  public loadingWrapper(fn: any, args: any[]) {
    this.loading.value = true;
    return fn(...args).finally(() => {
      this.loading.value = false;
    });
  }
}
